package com.tjn.methods;

/**
 * @author 陶镓妮
 * @version 1.0
 */
public class ThreadMethod03Daemon {
    public static void main(String[] args) throws InterruptedException {
        MyDaemon myDaemon = new MyDaemon();
        //设置myDaemon线程为守护线程,当所以线程都结束后，就算myDaemon线程没有执行完也会结束
        //如果没有设置，所有线程结束后myDaemon线程会一直运行，知道运行结束，要是死循环的话就不会结束
        myDaemon.setDaemon(true);
        myDaemon.start();
        for (int i = 0; i < 100; i++) {
            Thread.sleep(50);
            System.out.println("主线程运行中" + i);
        }
    }
}
class MyDaemon extends Thread{
    @Override
    public void run() {
        for (int i = 0; i < 200; i++){
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("子线程运行中" + i);
        }
    }
}